<?php
include "../../../lib/list.php";
$prdreview		= "INSERT INTO `uatp_prdreview`
					(`s_no`, `admin_name`, `p_no`, `count`, `price`, `unit`, `date`)
					VALUES (%d, '%s', %d, %d, %d, '%s','%s');";
$prdgroup 		= "INSERT INTO `uatp_prdgroup`(`s_no`,`unit`,`date`,`note`) VALUE(%d,'%s','%s','%s');";
$date			= 	date("Y-m-d H:i:s",time());

if($_POST['mode']=='insert'){
$p_no			=	$_POST['p_no'];
$s_no 			=	$_POST['s_no'];
$count 			=	$_POST['count'];
$price			=	$_POST['price'];
$admin_name		=	$_SESSION['IS_NAME'];
for($i=0;$i<count($p_no);$i++){
	$price[$i]	=	$count[$i] * $price[$i];
		if(!$s_no){
			echo("
			    <script>
			      window.alert('잘못된 점포입니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$p_no[$i]){
			echo("
			    <script>
			      window.alert('선택된 상품이 없습니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$admin_name){
			echo("
			    <script>
			      window.alert('잘못된 담당자입니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$count[$i]){
			echo("
			    <script>
			      window.alert('상품 갯수를 적어주세요.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
			}
			
		else{
			$query = sprintf($prdreview,
			clean($s_no),
			clean($admin_name),
			clean($p_no[$i]),
			clean($count[$i]),
			clean($price[$i]),
			'0',
			$date);
			DBquery($query);
		}
}
			$qry = sprintf($prdgroup,clean($s_no),'0',$date,'');
			DBquery($qry);
					
			mysql_close();
			
			echo("
					    <script>
			  			      window.alert('정상 입력 됐습니다.');
					         </script>"
				);
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
		

}


if($_POST['mode']=='defect'){
$s_no 			=	$_POST['s_no'];
$p_no			=	$_POST['p_no'];
$count 			=	$_POST['count'];
$price			=	$_POST['price'];
$note			=	$_POST['note'];
$admin_name		=	$_SESSION['IS_NAME'];
for($i=0;$i<count($p_no);$i++){
	$count[$i]	=	$count[$i] * -1;
	$price[$i]	=	$count[$i] * $price[$i];
		if(!$s_no){
			echo("
			    <script>
			      window.alert('잘못된 점포입니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$p_no[$i]){
			echo("
			    <script>
			      window.alert('선택된 상품이 없습니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$admin_name){
			echo("
			    <script>
			      window.alert('잘못된 담당자입니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$count[$i]){
			echo("
			    <script>
			      window.alert('상품 갯수를 적어주세요.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
			}
		if(!$note){
			echo("
			    <script>
			      window.alert('내용을 입력해주세요');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
			}
	
	$query = "SELECT * FROM uatp_product WHERE no = $p_no[$i]";
	$product = DBarray($query);
	if($product['minus']!=1){
		$query2 = "SELECT * FROM uatp_stock WHERE s_no = $s_no	AND p_no = $p_no[$i];";
		$stock = DBarray($query2);
		$chk_minus = $stock['count'] - $count[$i];
		
		if($chk_minus < 0){
			echo("
 						  <script>
					      window.alert('마이너스키가 활성화 되지않았습니다.\\n상품 마스터에서 마이너스키 활성화를 해주세요.');
					      </script>");
			mysql_close();
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
			}else{	
				$qry = sprintf($prdreview,
				clean($s_no),
				clean($admin_name),
				clean($p_no[$i]),
				clean($count[$i]),
				clean($price[$i]),
				'1',
				$date);
				DBquery($qry);
			}
		}else{	$qry = sprintf($prdreview,
				clean($s_no),
				clean($admin_name),
				clean($p_no[$i]),
				clean($count[$i]),
				clean($price[$i]),
				'1',
				$date);
				DBquery($qry);		
			}
}
		$qry = sprintf($prdgroup,clean($s_no),'1',$date,clean(str_html($note)));
		DBquery($qry);
mysql_close();
echo("
					    <script>
			  			      window.alert('정상 입력 됐습니다.');
					         </script>"
);
echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
}



if($_POST['mode']=='move'){
$p_no			=	$_POST['p_no'];
$count 			=	$_POST['count'];
$price			=	$_POST['price'];
$note			=	$_POST['note'];
$admin_name		=	$_SESSION['IS_NAME'];
$s_no		=	$_POST['s_no'];
$in_s_no		=	$_POST['in_s_no'];
$admin_name		=	$_SESSION['IS_NAME'];

for($i=0;$i<count($p_no);$i++){
	$price[$i]	=	$count[$i] * $price[$i];
		if(!$in_s_no){
			echo("
			    <script>
			      window.alert('상품을 받을  점포가 선택되지 않았습니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$s_no){
			echo("
			    <script>
			      window.alert('잘못된 점포입니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$p_no[$i]){
			echo("
			    <script>
			      window.alert('선택된 상품이 없습니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$admin_name){
			echo("
			    <script>
			      window.alert('잘못된 담당자입니다.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
		}
		if(!$count[$i]){
			echo("
			    <script>
			      window.alert('상품 갯수를 적어주세요.');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
			}
		if(!$note){
			echo("
			    <script>
			      window.alert('내용을 입력해주세요');
			      </script>");
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
			}
			
		else{
	
			$query = "SELECT * FROM uatp_stock WHERE s_no = $s_no AND p_no = $p_no[$i];";
			$stock = DBarray($query);
			$chk_minus = $stock['count'] - $count[$i];
			if($chk_minus < 0){
			echo("
 						  <script>
					      window.alert('재고가 부족합니다.');
					      </script>");
			mysql_close();
			echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
			return;
			}else{
				$in_query = sprintf("INSERT INTO `uatp_prdreview`
					(`s_no`, `admin_name`, `p_no`, `count`, `price`, `unit`, `date`, `s_no2`)
					VALUES (%d, '%s', %d, %d, %d, '%s', '%s', %d);",
				clean($in_s_no),
				clean($admin_name),
				clean($p_no[$i]),
				clean($count[$i]),
				clean($price[$i]),
				'2',
				$date,
				clean($s_no));
				DBquery($in_query);
				
				$query = sprintf("INSERT INTO `uatp_prdreview`
					(`s_no`, `admin_name`, `p_no`, `count`, `price`, `unit`, `date`, `s_no2`)
					VALUES (%d, '%s', %d, %d, %d, '%s', '%s', %d);",
				clean($s_no),
				clean($admin_name),
				clean($p_no[$i]),
				clean(-$count[$i]),
				clean(-$price[$i]),
				'2',
				$date,
				clean($in_s_no));
				DBquery($query);
				}
		}
		}
		$qry = sprintf($prdgroup,clean($s_no),'2',$date,clean(str_html($note)));
		DBquery($qry);
		$in_qry = sprintf($prdgroup,clean($in_s_no),'2',$date,clean(str_html($note)));
		DBquery($in_qry);
mysql_close();
echo("
					    <script>
			  			      window.alert('정상 입력 됐습니다.');
					         </script>"
);
echo "<meta http-equiv='refresh' content='0; url=$referer_domain'>";
}
?>